package com.app.teaware.common;

import android.content.res.Resources;
import androidx.recyclerview.widget.RecyclerView;

import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.app.teaware.bean.BaseListDataListBean;
import com.app.teaware.R;
import com.app.teaware.basis.BasisActivity;
import com.app.teaware.bean.BaseListData;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import my.CheckUtils;
import my.IntentUtils;

public class AdapterCommonDetail extends RecyclerView.Adapter<AdapterCommonDetail.ViewHolder> {

    BasisActivity mContext;
    BaseListDataListBean mListBean;

    boolean isTopPadding = true;


        public AdapterCommonDetail(BasisActivity context, BaseListDataListBean listBean) {
            mContext = context;
            mListBean = listBean;
        }
        public AdapterCommonDetail(BasisActivity context, BaseListDataListBean listBean,boolean isTopPadding) {
            mContext = context;
            mListBean = listBean;
            this.isTopPadding = isTopPadding;
        }


    //创建新View，被LayoutManager所调用
        @Override
        public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.common_detail_list_item, viewGroup, false);
            return new ViewHolder(view);
        }


        //将数据与界面进行绑定的操作
        @Override
        public void onBindViewHolder(ViewHolder viewHolder, int position) {
            viewHolder.bind(position);
        }

        //获取数据的数量
        @Override
        public int getItemCount() {
            return mListBean == null ? 0 : mListBean.getList().size();
        }

        //自定义的ViewHolder，持有每个Item的的所有界面元素
        public class ViewHolder extends RecyclerView.ViewHolder {
            @BindView(R.id.list_item)
            public View list_item;
            @BindView(R.id.text_name)
            public TextView text_name;
            @BindView(R.id.text_value)
            public TextView text_value;
            @BindView(R.id.img_icon)
            public ImageView img_icon;

            public ViewHolder(View view) {
                super(view);
                ButterKnife.bind(this, view);
            }

            public void bind(int position){
                BaseListData data = mListBean.getList().get(position);
                text_name.setText(data.getName());
                text_value.setText(data.getId());
                list_item.setTag(position);

                if(!isTopPadding && position == 0){
                    Resources res = mContext.getResources();
                            list_item.setPadding(res.getDimensionPixelSize(R.dimen.base_padding_mid),0,res.getDimensionPixelSize(R.dimen.base_padding_mid),res.getDimensionPixelSize(R.dimen.base_padding_xsmall));
                }

                if(CheckUtils.isPhone(data.getId())){
                    img_icon.setVisibility(View.VISIBLE);
                }else {
                    img_icon.setVisibility(View.GONE);
                }
                img_icon.setTag(position);

                if(!TextUtils.isEmpty(data.getName()) && !TextUtils.isEmpty(data.getId())){
                    if(data.getName().equals("是否需要驾驶员") && data.getId().equals("否")){
                        SpannableStringBuilder style = new SpannableStringBuilder("否（无需驾驶员则默认自己驾驶）");
                        style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.base_text_black)), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        style.setSpan(new ForegroundColorSpan(mContext.getResources().getColor(R.color.car_text_orange)), 1, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        style.setSpan(new RelativeSizeSpan(0.8f), 1, style.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                        text_value.setText(style);
                    }
                }
            }

            @OnClick(R.id.img_icon)
            void onImgClick(View view){
                int position = (int) view.getTag();
                BaseListData data = mListBean.getList().get(position);
                if(CheckUtils.isPhone(data.getId())){
                    IntentUtils.callToDial(mContext,data.getId());
                }
            }
        }
    }